package com.wowangz.cms.common.utils;

import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;
import org.joda.time.DateTime;

/**
 * 序列生成器
 * @ClassName: SeriesGenerater  
 * @author Green.Long
 * @date 2016年8月24日 下午12:17:08
 */
public class SeriesGenerater {
	//比写char数组简单
	private static final String allLetterStr = "ABCDEFGHJKLMNPQRSTUVWXYZ";
	private static final String allNumStr = "0123456789";
	
	/**
	 * 数字在前，字母在后
	 */
	private static final String allNumLetterStr = allNumStr + allLetterStr;
	
	/**
	 * 字母
	 */
	private static final char[] allLetter = allLetterStr.toCharArray();
	/**
	 * 数字
	 */
	private static final char[] allNum = allNumStr.toCharArray();
	
	private static final Random random = new Random();

	/**
	 * 获取指定位数的数字+字母组合随机序列
	 * @param length
	 * @return
	 */
	public static String randomLetterNum(int length) {
		char[] result = new char[length];
		
		for(int i = 0; i < length; ++i) {
			boolean isNum = random.nextBoolean();
			
			if(isNum) {//数字随机
				result[i] = randomNum();
			} else {//字母随机
				result[i] = randomLetter();
			}
		}
		
		return new String(result);
	}
	
	/**
	 * 获取一位数字随机
	 * @return
	 */
	public static char randomNum() {
		int index = random.nextInt(allNum.length);
		return allNum[index];
	}
	
	/**
	 * 获取一位字母随机
	 * @return
	 */
	public static char randomLetter() {
		int index = random.nextInt(allLetter.length);
		return allLetter[index];
	}
	
	/**
	 * 下一个编号
	 * 
	 * @param maxCode
	 * @param codeLen
	 * @return
	 */
	public static String nextKornet(String maxCode, int codeLen) {
		StringBuilder sbComplement = new StringBuilder();

		// 如果没有最大值
		if (maxCode == null) {
			maxCode = "";
		}
		int length = maxCode.length();
		if (length > codeLen) {
			throw new IllegalArgumentException(String.format("编号长度(%s)大于指定长度(%s)", length, codeLen));
		}
		// 补齐到指定长度
		for (int i = 0; i < codeLen - length; i++) {
			sbComplement.append(allNumLetterStr.charAt(0));
		}
		maxCode = sbComplement.append(maxCode).toString();

		char[] result = new char[codeLen];
		boolean isOk = false;
		for (int i = codeLen - 1; i >= 0; --i) {// 从最后一位开始处理
			int index = allNumLetterStr.indexOf(maxCode.charAt(i));

			if (!isOk) {
				index += 1;
				if (index >= allNumLetterStr.length()) {
					index = 0;
				} else {
					isOk = true;
				}
			}

			result[i] = allNumLetterStr.charAt(index);
		}

		if (!isOk) {
			throw new IllegalArgumentException(String.format("%s已经达到%s位上限", maxCode, codeLen));
		}

		return new String(result);
	}
	
	/**
	 * 生成验证码
	 */
	public static String genRandomNum(int length) {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Random rand = new Random();
        for (int i = 9; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < length; i++) {
            result = result * 10 + array[i];
        }
        return String.valueOf(result);
    }
	
	/**
     * 获取订单号<br />
     * 规则：X-yyMMddHHmmss-00000<br />
     * X: 1-android, 2-ios, 0-其他
     * yyMMddHHmmss: 时间
     * 00000: 5位随机数-->有重复的几率
     * TODO 这里要改，使用reids+分布式锁机制
     * @return
     */
    public static String getOutTradeNO() {
    	String x = "0";//TODO 目前都设置为0
        String dateStr = new DateTime().toString("yyMMddHHmmss");
        String ran = RandomStringUtils.random(5, false, true);
        
        String key = String.format("%s-%s-%s", x, dateStr, ran);
        return key;
    }
}
